/*
	Copyright 2008, 2009, 2010 Brightworks, Inc.
	
	This file is part of Language Mentor.
	
	Language Mentor is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	Language Mentor is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with Language Mentor.  If not, see <http://www.gnu.org/licenses/>.
*/
package com.langcollab.languagementor.controller 
{
    import com.brightworks.util.Debug;
    import com.langcollab.languagementor.model.MainModel;
    import com.langcollab.languagementor.service.Delegate_DownloadFileText;
    import com.langcollab.languagementor.service.IDelegate_DownloadFileText;
    
    import flash.events.Event;
    
    import mx.rpc.AsyncToken;
    import mx.rpc.events.ResultEvent;
    
    import org.swizframework.utils.commands.IEventAwareCommand;
    import org.swizframework.utils.services.ServiceHelper;

    public class Command_DownloadLearningModeDescriptions extends Command_Base implements IEventAwareCommand
    {
		[Inject]
		public var delegate:IDelegate_DownloadFileText; 
		
		[Inject]
		public var model:MainModel;
		
		[Inject]
		public var serviceHelper:ServiceHelper;
		
        private var _activeHttpRequests:Array = new Array();  // contains learningModeIds
        private var _result:Object = new Object();            // props are learningModeIds, values are HTML strings

        public function Command_DownloadLearningModeDescriptions() {
            super();
        }

        // ****************************************************
        //
        //
        //          Public Methods
        //
        //
        // ****************************************************

		public function execute():void {
            var learningModeIds:Array = model.dataManager.getLearningModeIDListSortedByLocationInOrder();
            for each(var learningModeId:int in learningModeIds) {
                var modeToken:String = model.dataManager.getLearningModeTokenFromID(learningModeId);
                var url:String = "html/" + model.dataManager.getNativeLanguageResource("iso639_3Code") + "/" + modeToken + ".html";
				serviceHelper.executeServiceCall(delegate.download(url), result_Download, fault, [learningModeId]);
                _activeHttpRequests.push(learningModeId);
            }
        }
		
		// ****************************************************
		//
		//
		//          Private Methods
		//
		//
		// ****************************************************
		
        private function result_Download(data:Object, learningModeId:int):void {
			//// implement user-friendly debug call
            if (_activeHttpRequests.indexOf(learningModeId) == -1)
				Debug.error_Fatal(["Command_DownloadLearningModeDescriptions.result(): Result returned for learningModeId without an active HTTP request:", data]);
            _activeHttpRequests.splice(_activeHttpRequests.indexOf(learningModeId), 1);
            _result[learningModeId] = ResultEvent(data).result;
            if (_activeHttpRequests.length == 0) {
				model.learningModeDescriptionsHTML = _result;
                super.result(null);
            }
        }
    }
}
















